/**
 *  Copyright (C) 2011  Thomas ROBIN
 * 
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package triseries;

import java.io.File;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author tshepo
 * 
 */
public class VideoFile {

	private static final boolean DEBUG = (Main.debug || MyProperties.debug.equalsIgnoreCase("VideoFile"));
	private static Pattern paternSeasonAndEpisode = Pattern.compile(".*");
	private static Pattern paternFilter = Pattern.compile(".*");
	private static String format = "";
	private static boolean dotsInFormat;
	/**
	 * Episode number of the videoFile
	 */
	private int nb;
	/**
	 * season of the videoFile
	 */
	private int season;
	/**
	 * Show of the videoFile
	 */
	private String show;
	/**
	 * Title of the videoFile
	 */
	private String title = "";
	/**
	 * File of the videoFile
	 */
	private File file;
	private String extention;
	private boolean istvshow;
	public String reste;
	private String url = "";
	private String path = "";
	private String basename = "";

	/**
	 * Create un videoFile
	 * 
	 * @param _file
	 */
	public VideoFile(File _file) {
		if(DEBUG) System.err.println("VideoFile(File "+_file.getName()+")");
		file = _file;
		parseName();
	}

	public VideoFile(String _import) {
		if(DEBUG) System.err.println("VideoFile(String "+_import+")");
		String[] tab = _import.split("\\}\\:\\:\\{");
		file = new File(tab[0]);
		parseName();
		show = tab[1];
		title = tab[2];
		url = tab[3];

	}

	private void parseName() {
		if(DEBUG) System.err.println("VideoFile.parseName() - "+ file.getName());
		String fichier = file.getName();
		if (paternFilter.matcher(fichier).matches()) {
			istvshow = true;
			extention = fichier.substring(fichier.lastIndexOf(".") + 1);
			show = paternSeasonAndEpisode.split(fichier)[0].replace(".", " ")
					.replace("_", " ").trim();
			reste = paternSeasonAndEpisode.split(fichier)[1];
			reste = reste.substring(reste.lastIndexOf(".") + 1);

			Matcher m = paternSeasonAndEpisode.matcher(fichier);
			m.find();
			m = Pattern.compile("[0-9][0-9]*").matcher(m.group());
			m.find();
			season = Integer.parseInt(m.group());
			m.find();
			nb = Integer.parseInt(m.group());

		} else {
			istvshow = false;
		}

	}

	public void setTitle(String t) {
		title = t;
	}

	public void setShow(String t) {
		show = t.trim();
	}

	public void setUrl(String t) {
		url = t.trim();
	}

	@Override
	public String toString() {
		if(DEBUG) System.err.println("VideoFile.toString() - "+ format + " " +show + season+"x"+nb);
		if (istvshow) {
			String returnString = format.replace("{SERIE}", show);
			returnString = returnString.replace("{SAISON}", String
					.valueOf(season));
			returnString = returnString.replace("{EPISODE}", String.format(
					"%02d", nb));
			returnString = returnString.replace("{TITRE}", title);
			returnString = returnString.replaceAll(
					"(\\\")|(\\<)|(\\>)|(\\:)|(\\*)|(\\?)|(\\|)", "");
			if (dotsInFormat) {
				return returnString.replace(" ", ".") + "." + extention;
			} else {
				return returnString + "." + extention;
			}
		} else {
			return file.getName();

		}
	}

	public static String makeName(String show, int season, int nb, String title) {
		if(DEBUG) System.err.println("VideoFile.makeName() - "+ format + " " +show + season+"x"+nb);
		String returnString = format.replace("{SERIE}", show);
		returnString = returnString.replace("{SAISON}", String.valueOf(season));
		returnString = returnString.replace("{EPISODE}", String.format("%02d",
				nb));
		returnString = returnString.replace("{TITRE}", title);
		returnString = returnString.replaceAll(
				"(\\\")|(\\<)|(\\>)|(\\:)|(\\*)|(\\?)|(\\|)", "");
		if (dotsInFormat) {
			return returnString.replace(" ", ".");
		} else {
			return returnString;
		}
	}

	public String basename() {
		if(DEBUG) System.err.println("VideoFile.basename() - "+ format + " " +show + season+"x"+nb);
		if (basename.isEmpty())
			if (istvshow) {
				String returnString = format.substring(format
						.lastIndexOf(File.separator) + 1);
				returnString = returnString.replace("{SERIE}", show);
				returnString = returnString.replace("{SAISON}", String
						.valueOf(season));
				returnString = returnString.replace("{EPISODE}", String.format(
						"%02d", nb));
				returnString = returnString.replace("{TITRE}", title);
				returnString = returnString.replaceAll(
						"(\\\")|(\\<)|(\\>)|(\\:)|(\\*)|(\\?)|(\\|)", "");
				if (dotsInFormat) {
					basename = returnString.replace(" ", ".");
				} else {
					basename = returnString;
				}
			} else {
				String s = file.getName().substring(0,
						file.getName().lastIndexOf("."));
				basename = s.substring(s.lastIndexOf(File.separator));
			}
		return basename;

	}

	public String path() {
		if(DEBUG) System.err.println("VideoFile.path() - "+ format + " " +show + season+"x"+nb);
		if (path.isEmpty())
			if (istvshow) {
				String returnString = format.substring(0, format
						.lastIndexOf(File.separator));
				returnString = returnString.replace("{SERIE}", show);
				returnString = returnString.replace("{SAISON}", String
						.valueOf(season));
				returnString = returnString.replace("{EPISODE}", String.format(
						"%02d", nb));
				returnString = returnString.replace("{TITRE}", title);
				returnString = returnString.replaceAll(
						"(\\\")|(\\<)|(\\>)|(\\:)|(\\*)|(\\?)|(\\|)", "");
				if (dotsInFormat) {
					path = returnString.replace(" ", ".");
				} else {
					path = returnString;
				}
			} else {
				path = file.getName().substring(0,
						file.getName().lastIndexOf(File.separator));
			}
		return path;
	}

	public String getShow() {
		return show
				.replaceAll("(\\\")|(\\<)|(\\>)|(\\:)|(\\*)|(\\?)|(\\|)", "");
	}

	public String getTitle() {
		return title.replaceAll("(\\\")|(\\<)|(\\>)|(\\:)|(\\*)|(\\?)|(\\|)",
				"");
	}

	public String getUrl() {
		return url;
	}

	public String getSeason() {
		return String.valueOf(season);
	}

	public String getEpisode() {
		return String.valueOf(nb);
	}

	public boolean isTVShow() {
		return istvshow;

	}

	public static void SetParterns(String exts, String seasonAndEpisode) {
		paternSeasonAndEpisode = Pattern.compile(seasonAndEpisode);
		paternFilter = Pattern.compile("(.*)(" + seasonAndEpisode
				+ ")(.*)(\\.("
				+ (exts + "," + exts.toUpperCase()).replace(",", ")|(") + "))");

	}

	public static void SetFormat(String f, boolean dots) {
		format = f;
		dotsInFormat = dots;

	}

	public File file() {
		return file;
	}

	String extention() {
		return extention;
	}

	public String export() {
		if(DEBUG) System.err.println("VideoFile.export() - "+file.getName()+" "+url);
		return file.getName() + "}::{" + show + "}::{" + title + "}::{" + url;
	}
}
